package client

import (
	"github.com/mars/internal/admin/core/domain"
)

// AddressView 用于前端展示的地址信息
type AddressView struct {
	ID         string `json:"id"`
	Name       string `json:"name"`
	Phone      string `json:"phone"`
	Province   string `json:"province"`
	City       string `json:"city"`
	District   string `json:"district"`
	Detail     string `json:"detail"`
	IsDefault  bool   `json:"isDefault"`
	CreateTime string `json:"createTime"` // 格式化的时间字符串
	UpdateTime string `json:"updateTime"` // 格式化的时间字符串
}

// VipInfoView 用于前端展示的VIP信息
type VipInfoView struct {
	Type        string  `json:"type"`
	ActivatedAt string  `json:"activatedAt"`         // 格式化的时间字符串
	ExpiresAt   *string `json:"expiresAt,omitempty"` // 格式化的时间字符串
	OrderID     string  `json:"orderId"`
	IsActive    bool    `json:"isActive"`
}

// UserView 用于前端展示的用户信息
type UserView struct {
	ID            string        `json:"_id,omitempty"`
	OpenID        string        `json:"openid"`
	NickName      string        `json:"nickName"`
	Phone         string        `json:"phone"`
	AvatarUrl     string        `json:"avatarUrl"`
	Gender        int           `json:"gender"`
	Country       string        `json:"country"`
	Province      string        `json:"province"`
	City          string        `json:"city"`
	Addresses     []AddressView `json:"addresses"`
	Role          string        `json:"role"`
	Status        string        `json:"status"`
	VipInfo       *VipInfoView  `json:"vipInfo,omitempty"`
	CreateTime    string        `json:"createTime"`    // 格式化的时间字符串
	UpdateTime    string        `json:"updateTime"`    // 格式化的时间字符串
	LastLoginTime string        `json:"lastLoginTime"` // 格式化的时间字符串
}

// ToUserView 将domain.User转换为UserView
func ToUserView(user *domain.User) *UserView {
	if user == nil {
		return nil
	}

	view := &UserView{
		ID:            user.ID,
		OpenID:        user.OpenID,
		NickName:      user.NickName,
		Phone:         user.Phone,
		AvatarUrl:     user.AvatarUrl,
		Gender:        user.Gender,
		Country:       user.Country,
		Province:      user.Province,
		City:          user.City,
		Role:          user.Role,
		Status:        user.Status,
		CreateTime:    formatTimestamp(user.CreateTime, TimeFormatDateTime),
		UpdateTime:    formatTimestamp(user.UpdateTime, TimeFormatDateTime),
		LastLoginTime: formatTimestamp(user.LastLoginTime, TimeFormatDateTime),
	}

	// 转换地址列表
	view.Addresses = make([]AddressView, len(user.Addresses))
	for i, address := range user.Addresses {
		view.Addresses[i] = AddressView{
			ID:         address.ID,
			Name:       address.Name,
			Phone:      address.Phone,
			Province:   address.Province,
			City:       address.City,
			District:   address.District,
			Detail:     address.Detail,
			IsDefault:  address.IsDefault,
			CreateTime: formatTimestamp(address.CreateTime, TimeFormatDateTime),
			UpdateTime: formatTimestamp(address.UpdateTime, TimeFormatDateTime),
		}
	}

	// 转换VIP信息
	if user.VipInfo != nil {
		view.VipInfo = &VipInfoView{
			Type:        user.VipInfo.Type,
			ActivatedAt: formatTimestamp(user.VipInfo.ActivatedAt, TimeFormatDateTime),
			ExpiresAt:   formatTimestampPtr(user.VipInfo.ExpiresAt, TimeFormatDateTime),
			OrderID:     user.VipInfo.OrderID,
			IsActive:    user.VipInfo.IsActive,
		}
	}

	return view
}

// ToUserViewList 将domain.User列表转换为UserView列表
func ToUserViewList(users []domain.User) []UserView {
	views := make([]UserView, len(users))
	for i, user := range users {
		if view := ToUserView(&user); view != nil {
			views[i] = *view
		}
	}
	return views
}
